package com.sip.stream.activity;

import android.util.Log;
import android.view.WindowManager;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sip.stream.R;
import com.sip.stream.activity.preview.IPreviewActivity;
import com.sip.stream.activity.preview.PreviewFragment;
import com.sip.stream.base.BaseActivity;
import com.sip.stream.databinding.MainLayoutBinding;
import com.sip.stream.rxbus.RxBus;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.functions.Consumer;

public class MainActivity extends BaseActivity<MainLayoutBinding> implements IPreviewActivity {

    private static final String TAG = "MainActivity";

    private PreviewFragment previewFragmentInstance;

    @Override
    public void initPage() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        PermissionUtils.permission(PermissionConstants.STORAGE, PermissionConstants.CAMERA, PermissionConstants.LOCATION, PermissionConstants.MICROPHONE, PermissionConstants.SENSORS, PermissionConstants.PHONE).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {

            }

            @Override
            public void onDenied() {
                ToastUtils.showShort("权限未开启");
            }
        }).request();
    }


    /***
     * 权限开启后开启预览
     */
    private void afterPermissions(){
        Single.just(Boolean.TRUE).observeOn(Schedulers.io()).doOnSuccess(new Consumer() {
            @Override
            public  void accept(Object obj) {
                previewFragmentInstance = PreviewFragment.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.videoLayout,previewFragmentInstance).commit();
            }
        }).observeOn(AndroidSchedulers.mainThread()).doOnSuccess(new Consumer() {
            @Override
            public  void accept(Object obj) {

            }
        }).subscribe(new SingleObserver<Boolean>() {
            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
            }

            @Override
            public void onSubscribe(Disposable disposable) {

            }

            @Override
            public void onSuccess(Boolean bool) {

            }
        });
    }

    @Override
    public Single<Boolean> backToPreview() {
        return Single.just(Boolean.TRUE);
    }

    @Override
    public void handleMediaAction(String action) {
        Log.d(TAG, "handleMediaAction " + action);
        if (previewFragmentInstance != null) {
            previewFragmentInstance.handleMediaAction(action);
        }
    }

    @Override
    public boolean isOpenedCamera() {
        if (previewFragmentInstance != null) {
            return previewFragmentInstance.isOpenedCamera();
        }
        Log.w(TAG, "isOpenedCamera previewFragment == null");
        return false;
    }

    @Override
    public void onMarkEvent(boolean status) {

    }

    @Override
    public boolean startPreview() {
        return false;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
